<?php
require_once(dirname(__FILE__) . '/base.class.php');
class Order extends Base
{
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        echo json_encode($output);
    }


    /*
        获取订单列表
    */
    function lists()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        // 获取页码
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = 1;
        }
        // 每页显示数据条数
        if (isset($_GET['page_num'])) {
            $page_num = $_GET['page_num'];
        } else {
            $page_num = 12;
        }
        // 起始位置
        $start = ($page - 1) * $page_num;
        $sql = "select * from orders limit {$start},{$page_num}";
        $list = $this->db->query($sql);
        foreach ($list as $key => $item) {
            $order_items = $this->db->query("select * from order_items where order_id={$item['id']}");
            if ($order_items) {
                // 将图片路径处理成绝对路径
                foreach ($order_items as $k => $item) {
                    $order_items[$k]['product_cover'] = path($item['product_cover']);
                }
                $list[$key]['items'] = $order_items;
            }
        }
        $total = $this->db->get_one("select count(id) as count from orders")['count'];
        $output['data'] = ['list' => $list, 'total' => $total];
        echo json_encode($output);
    }


    /*
        获取订单详情
    */
    function detail()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (isset($_GET['id'])) {
            $order_id = $_GET['id'];
            $sql = "select * from  orders  where id={$order_id}";
            $order = $this->db->get_one($sql);
            if ($order) {
                $order_items = $this->db->query("select * from order_items where order_id={$order_id}");
                if ($order_items) {
                    // 将图片路径处理成绝对路径
                    foreach ($order_items as $key => $item) {
                        $order_items[$key]['product_cover'] = path($item['product_cover']);
                    }
                    $order['items'] = $order_items;
                }
                $output['data'] = $order;
            } else {
                $output['errcode'] = 3;
                $output['msg'] = '获取订单信息失败';
            }
        } else {
            $output['errcode'] = 2;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }

    /*
        删除订单
    */
    function del()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (isset($_GET['id'])) {
            $order_id = $_GET['id'];
            if ($order_id > 69) {
                $sql = "delete from  orders  where id={$order_id}";
                $res = $this->db->query($sql);
                if ($res) {
                    // 删除订单商品
                    $this->db->query("delete from order_items where order_id={$order_id}");
                }
            }
        } else {
            $output['errcode'] = 2;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }


    /*
        更新订单状态
    */
    function update()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($_POST) {
            // 获取订单id
            $order_id = $_GET['id'];
            $queryStr = arr2str($_POST);
            $sql = "update orders set {$queryStr} where id={$order_id}";
            $res = $this->db->query($sql);
        } else {
            $output['errcode'] = 2;
            $output['msg'] = '参数错误';
        }
        $output['sql'] = $sql;
        echo json_encode($output);
    }
}
